package com.myweb.provider.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.myweb.facade.service.EasyService;
import com.myweb.model.EasyTree;
import com.myweb.model.EasyTreeModel;
import com.myweb.utils.ConvertUtils;
import com.myweb.utils.ReflectUtil;


/**
 * 树结构查询
 * @author 张栋  2016年4月28日下午10:24:18
 */
@Service
@Transactional
public class EasyServiceImpl implements EasyService{
	
	public List<EasyTree> easyTree(List all, EasyTreeModel EasyTreeModel, List in, boolean recursive , boolean open) {
		List<EasyTree> trees = new ArrayList<EasyTree>();
		for (Object obj : all) {
			trees.add(easyTree(obj, EasyTreeModel, in, recursive,open));
		}
		return trees;
	}
	
	private EasyTree easyTree(Object obj, EasyTreeModel EasyTreeModel, List in, boolean recursive,boolean open) {
		EasyTree tree = new EasyTree();
		Map<String, Object> attributes = new HashMap<String, Object>();
		ReflectUtil reflectHelper = new ReflectUtil(obj);
		String id = ConvertUtils.getString(reflectHelper.getMethodValue(EasyTreeModel.getIdField()));
		tree.setId(id);
		tree.setText(ConvertUtils.getString(reflectHelper.getMethodValue(EasyTreeModel.getTextField())));
		if(EasyTreeModel.getCodeField()!=null){
			tree.setCode(ConvertUtils.getString(reflectHelper.getMethodValue(EasyTreeModel.getCodeField())));
		}
		if(EasyTreeModel.getSrcField()!=null){
			tree.setHref(ConvertUtils.getString(reflectHelper.getMethodValue(EasyTreeModel.getSrcField())));
		}
		if(EasyTreeModel.getSortField()!=null){
			tree.setSortnum(ConvertUtils.getInt(reflectHelper.getMethodValue(EasyTreeModel.getSortField()),0));
		}
		if(EasyTreeModel.getTypeField() != null) {
			tree.setType(ConvertUtils.getInt(reflectHelper.getMethodValue(EasyTreeModel.getTypeField()),0));
		}
		
		Map<String, String> attributes2 = EasyTreeModel.getAttributes();
		if(attributes2!=null){
			for(Map.Entry<String, String> entry : attributes2.entrySet()){
				attributes.put(entry.getKey(), ConvertUtils.getString(reflectHelper.getMethodValue(entry.getValue())));
			}
			tree.setAttributes(attributes);
		}

		
		if (in != null) {
			if (in.size() > 0) {
				for (Object inobj : in) {
					ReflectUtil reflectHelper2 = new ReflectUtil(inobj);
					String inId = ConvertUtils.getString(reflectHelper2.getMethodValue(EasyTreeModel.getIdField()));
                    if (inId.equals(id)) {
						tree.setChecked(true);
					}
				}
			}
		}
		
		
		List curChildList = (List) reflectHelper.getMethodValue(EasyTreeModel.getChildField());
		if (curChildList != null && curChildList.size() > 0) {
			if(open){
				tree.setState("open");
			}else{
				tree.setState("closed");
			}
			tree.setChecked(false);

            if (recursive) { // 递归查询子节点
                List<EasyTree> children = new ArrayList<EasyTree>();
                List nextChildList = new ArrayList(curChildList);
                for (Object childObj : nextChildList) {
                	open = false;
                    EasyTree t = easyTree(childObj, EasyTreeModel, in, recursive,open);
                    children.add(t);
                }
                tree.setChildren(children);
            }
        }
		return tree;
	}
	
}
